/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.oracleandsxnd.erp.purchase.service;

import com.oracleandsxnd.erp.common.persistence.Page;
import com.oracleandsxnd.erp.common.service.CrudService;
import com.oracleandsxnd.erp.common.utils.StringUtils;
import com.oracleandsxnd.erp.purchase.dao.PurchaseDetailDao;
import com.oracleandsxnd.erp.purchase.dao.PurchaseOrderDao;
import com.oracleandsxnd.erp.purchase.entity.PurchaseDetail;
import com.oracleandsxnd.erp.purchase.entity.PurchaseOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 采购单Service
 * @author 采购单
 * @version 2019-12-17
 */
@Service
@Transactional(readOnly = true)
public class PurchaseOrderService extends CrudService<PurchaseOrderDao, PurchaseOrder> {

	@Autowired
	private PurchaseDetailDao purchaseDetailDao;

	public PurchaseOrder get(String id) {
		PurchaseOrder purchaseOrder = super.get(id);
		purchaseOrder.setPurchaseDetailList(purchaseDetailDao.findList(new PurchaseDetail(purchaseOrder)));
		return purchaseOrder;
	}
	
	public List<PurchaseOrder> findList(PurchaseOrder purchaseOrder) {
		return super.findList(purchaseOrder);
	}
	
	public Page<PurchaseOrder> findPage(Page<PurchaseOrder> page, PurchaseOrder purchaseOrder) {
		return super.findPage(page, purchaseOrder);
	}
	
	@Transactional(readOnly = false)
	public void save(PurchaseOrder purchaseOrder) {
		super.save(purchaseOrder);
		for (PurchaseDetail purchaseDetail : purchaseOrder.getPurchaseDetailList()){
			if (purchaseDetail.getId() == null){
				continue;
			}
			purchaseDetail.setCompany(purchaseOrder.getCompany());
			if (PurchaseDetail.DEL_FLAG_NORMAL.equals(purchaseDetail.getDelFlag())){
				if (StringUtils.isBlank(purchaseDetail.getId())){
					purchaseDetail.setPurchaseId(purchaseOrder);
					purchaseDetail.preInsert();
					purchaseDetailDao.insert(purchaseDetail);
				}else{
					purchaseDetail.preUpdate();
					purchaseDetailDao.update(purchaseDetail);
				}
			}else{
				purchaseDetailDao.delete(purchaseDetail);
			}
		}
	}
	
	@Transactional(readOnly = false)
	public void delete(PurchaseOrder purchaseOrder) {
		super.delete(purchaseOrder);
		purchaseDetailDao.delete(new PurchaseDetail(purchaseOrder));
	}

	public Page<PurchaseDetail> findDPage(Page<PurchaseDetail> page, PurchaseDetail entity) {
		entity.setPage(page);
		page.setList(purchaseDetailDao.findList(entity));
		return page;
	}

	public Page<PurchaseDetail> findtjPage(Page<PurchaseDetail> page, PurchaseDetail entity) {
		List<PurchaseDetail> xsds=purchaseDetailDao.findtjList(entity);
		if(xsds!=null&&xsds.size()>0) {
			PurchaseDetail xsd = new PurchaseDetail();
			for (PurchaseDetail d:xsds
			) {
				xsd.setName("小计");
				xsd.setAmount(xsd.getAmount()+d.getAmount());
				xsd.setTotalprice(xsd.getTotalprice()+d.getTotalprice());

			}


			xsds.add(xsd);
		}
		entity.setPage(page);
		page.setList(xsds);
		return page;
	}

	public Page<PurchaseDetail> findpricePage(Page<PurchaseDetail> page, PurchaseDetail entity) {
		entity.setPage(page);
		List<PurchaseDetail> pds=purchaseDetailDao.findpriceList(entity);
		for (PurchaseDetail pd:pds
			 ) {
			PurchaseDetail p=purchaseDetailDao.findprice(pd);
			pd.setLowprice(p.getLowprice());
			pd.setHigprice(p.getHigprice());
			pd.setAvgprice(p.getAvgprice());
			pd.setNewprice(p.getNewprice());
		}
		page.setList(pds);
		return page;
	}
}